package com.wxg.controller;

import com.alibaba.fastjson.JSON;
import com.wxg.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
public class UserController {

    /**
     * 参考 : https://www.jianshu.com/p/2e6fd6d625cd
     *
     * @param user
     * @param bindingResult
     * @return
     */
    @RequestMapping("/test1")
    public Object test1(@Valid User user,
                        BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();

            log.info(JSON.toJSONString(allErrors));

            return allErrors;
        }

        log.info(user.toString());

        return "OK";
    }
}
